package com.study.minio.event;

import com.study.minio.service.OpenEmptyCardOrderService;
import com.study.minio.utils.MinIoUtils;
import io.minio.ObjectWriteResponse;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

@Slf4j
@Component
public class ApplicationStartRunner implements ApplicationRunner {

    @Autowired
    private OpenEmptyCardOrderService openEmptyCardOrderService;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("项目启动");
        String[] sourceArgs = args.getSourceArgs();
        for (String arg : sourceArgs) {
            System.out.print(arg + " ");
        }
        System.out.println();

//        openEmptyCardOrderService.process();
//        uploadFileTest();
    }



    final ExecutorService exec = Executors.newFixedThreadPool(15);
    private MinIoUtils minIoService = new MinIoUtils();


    public void uploadFileTest() throws InterruptedException {
        String bucketName = "test";
        // 检查存储桶是否已经存在
        boolean isExist = minIoService.bucketExists(bucketName);
        if (!isExist) {
            minIoService.makeBucket(bucketName);
            System.out.println("Bucket create." + bucketName);
        }

        for (int i = 400; i < 1000; i++) {
            final int count = i;
            exec.submit(() -> {
                int size = 1;
                //
                File dir = new File("/home/train/");//D:\img\baidu_ai\meter_det\train
                for (File file : dir.listFiles()) {
                    //objectName为路径名称
                    String objectName = count + "/" + file.getName();
                    if (Objects.isNull(file)) {
                        log.error("文件为空file={},objectName={}", file, objectName);
                        continue;
                    }


                    try (InputStream is = new FileInputStream(file);) {
                        if (Objects.isNull(is)) {
                            log.error("文件流为空is={},objectName={}", is, objectName);
                            continue;
                        }

                        long start = System.currentTimeMillis();
                        // 使用putObject上传一个文件到存储桶中。 图片要预览功能，要设置 contextType = "image/jpeg"
                        ObjectWriteResponse response = minIoService.putObject(bucketName, objectName, is, "image/png");
                        long end = System.currentTimeMillis();
                        //System.out.println(response.etag());

//                    InputStream inputStream = minIoService.getObject(bucketName, objectName);
//                    String getResult = Objects.nonNull(inputStream) ? "成功" : "失败";
                        log.info("{}文件{}大小{},上传时间{}ms ,{}", (size++), objectName, file.length(), end - start, response);
                    } catch (Exception e) {
                        log.error(e.getMessage(),e);
                    }
                    LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2));
                }
                log.info("{}处理完成", count);

            });
            LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(10));
        }

        //System.out.println("Game Over.");
        //log.info("Game Over.");
    }

}
